<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%@ include file="/commons/jsp/taglibs.jsp"%>
<%@taglib uri="http://www.framework.com/taglib" prefix="frame"%>
<%@taglib uri="http://www.framework.com/taglib/tree" prefix="tree"%>
  <head>
	<title>公共平台 </title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<!-- 样式文件的引入 -->
	<link href="${ctx}/commons/css/style.css" rel="stylesheet" type="text/css"/>
	<link rel="stylesheet" type="text/css" href="${ctx}/commons/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="${ctx}/commons/themes/icon.css">
	<!-- jqueryUI JS文件的引入 -->
	<script type="text/javascript" src="${ctx}/commons/js/jquery-1.8.0.min.js"></script>
	<script type="text/javascript" src="${ctx}/commons/js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="${ctx}/commons/js/easyui-extends-validate.js"></script>
	<!-- jqueryUI国际化 -->
	<script type="text/javascript" src="${ctx}/commons/js/easyui-lang-zh_CN.js"></script>
	<!-- 开发使用的公用js方法 -->
	<script type="text/javascript" src="${ctx}/commons/js/common.js"></script>
	<!-- 开发国际化的引用，主要完成后台信息在前台提示的国际化，一定要放在最后 -->
	<script type="text/javascript">var ctx = '${ctx}';var actionExt = "action";var i18n = new Object();</script>
	<script src="${ctx}/commons/js/i18n/message_zh_CN.js" type="text/javascript"></script>
  </head>
  
  <body>
    <div class="easyui-layout" data-options="fit:true,border:false" >
			<div data-options="region:'north',border:false" style="padding: 4px;height: 80px;overflow: hidden;">
			<div class="easyui-panel" data-options="fit:true,border:true" >
			<div  class="queryContent">
				<form name="searchForm" id="searchGoodsForm" method="post" action="" style="display: none; ">
				<table class="queryTable" >
					<tbody>
						<tr>
							<th width="50%" colspan="3"></th>
							<th width="50%" colspan="3">商品名称</th>
							<td >
							<input class="easyui-validatebox" type="text" name="goodsName" maxlength="20" style="width:120px"></input></td>
							
							<th align="right">商品编号</th>
							<td width="10%">
							<input class="easyui-validatebox" type="text" name="goodsCode" maxlength="32" style="width:120px"></input></td>
							
							<th align="right">商品类型</th>
							<td width="10%">
								<td><input name="pName" class="easyui-validatebox" readonly="readonly" id="pName" size="20" value="${goodsType.pName}" maxlength="50" />
									<input type="hidden" name="goodsTypeId" id="goodsTypePid"  size="30"/>
									<span id="pModel" style="width: 100px;height: 100px;"><a href="javascript:void(0);" onclick="selectParentgoodsType()"><img src="${ctx}/commons/themes/icons/icon5.png" border="0" title="商品类型"/></a></span>
							</td>
						</tr>
						<tr>
							<th width="50%" colspan="3"></th>
							<th width="50%" colspan="3">审核状态</th>
							<td width="10%">
							<select class="easyui-combobox" name="approveStatus" style="width:120px;" data-options="panelHeight:100">
								<option value="">-请选择状态-</option>
									<option value="1">待审核</option>
									<option value="2">已通过</option>
									<option value="3">不通过</option>
									<option value="4">不审核</option>
								</select></td>
							<th >上架状态</th>
							<td width="10%">
							<select class="easyui-combobox" name="isShelve" style="width:120px;" data-options="panelHeight:88">
								<option value="">-请选择状态-</option>
									<option value="0">待上架</option>
									<option value="1">已上架</option>
									<option value="2">已下架</option>
								</select></td>
							
							<th  >上架时间</th>
							<td colspan="4">
							 	<input id="startTime" class="easyui-datebox" type="text" name="startTime"  data-options="validType:['dateTimeLessCompare[\'endTime\']']" maxlength="20" style="width:120px"/>-<input id="endTime" class="easyui-datebox" type="text"  data-options="validType:['dateTimeMoreCompare[\'startTime\']']" name="endTime" maxlength="20" style="width:120px"/>
							</td>
							<td align="right"><a href="javascript:void(0);" class="easyui-linkbutton" id="searchGoods"  plain="true">查询</a>
							<a href="javascript:void(0);" class="easyui-linkbutton" id="resetsearchGoods" plain="true">清空</a></td>
						</tr>
					</tbody>
				</table>
				</form>
			</div>
			</div>
			</div>
			<div data-options="region:'center',border:false" style="padding: 4px;">
		    <table id="listGoodsTable" class="easyui-datagrid" 
				data-options="singleSelect:false,toolbar:'#goodstb',method:'post',rownumbers:true,fit:true,fitColumns:true,border:true,
					pagination:true,remoteSort:true,multiSort:true,striped:true,onLoadSuccess:function(data){$('#'+goodsParamObj.searchID).attr('disabled',false);},
					url:'${ctx}/business/goodsManage/list'"}>
				<thead>
					<tr>
						<th data-options="field:'id',checkbox:true"></th>
						<th data-options="field:'goodsName',width:100,formatter: function(value,row,index){
								return '<a href=${ctx}/business/goodsManage/input?goodsId='+row.id+'>'+value+'</a>';
							},title:'商品名称'"></th>
						<th data-options="field:'goodsCode',width:100,title:'商品编码',sortable:true"></th>
						<th data-options="field:'goodsTypeName',width:100,title:'商品类型',sortable:true" align="center"></th>
						<th data-options="field:'approveStatusName',width:80,title:'审核状态'" align="center"></th>
						<th data-options="field:'approverName',width:100,title:'审核人'" align="center"></th>
						<th data-options="field:'approveTime',width:100,title:'审核时间',sortable:true" align="center"></th>
						<th data-options="field:'isShelveName',width:100,title:'是否上架'" align="center"></th>
						<th data-options="field:'shelveTime',width:100,title:'上架时间',sortable:true" align="center"></th>
						<th data-options="field:'sellPrice',width:50,title:'价格(元)',sortable:true" align="center"></th>
						<th data-options="field:'goodsStorage',width:50,title:'库存(件)',sortable:true" align="center"></th>
						<th data-options="field:'options',width:150,title:'操作',formatter:getOptionsBtn" align="center"></th>
				</thead>
			</table>
			<div id="goodstb" style="display: none;">
				<div>
					<a href="${ctx}/business/goodsManage/input" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
<%--					<a href="javascript:void(0);" onclick="deleteData(goodsParamObj);" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
					<a href="javascript:void(0);" onclick="putGoodsOnShelve(1);" class="easyui-linkbutton" iconCls="icon-ok" plain="true">上架</a>--%>
					<a href="javascript:void(0);" onclick="putGoodsOnShelve(2);" class="easyui-linkbutton" iconCls="icon-cancel" plain="true">下架</a>
				</div>
			</div>
		</div>
	</div>
	<tree:goodsTypeTree ctx="${ctx}" selectMethod="single" goodsTypeNames="goodsTypeName" goodsTypeIds="goodsTypeId"/>
	<div id="bindPicDialog"></div>
	<div id="selPicDialog"></div>
</body>
	<script type="text/javascript">
		//初始化表格对应的js对象
		var goodsParamObj;
		$(function() {
			//初始化表格对象数据
			goodsParamObj = new initParams(
					{
						paramObjName : 'goodsParamObj',//表格对应的实体对象名称，好区分多个表格情况下的对象
						tableId:'listGoodsTable',
						addDataFormId:'addGoodsDataForm',
						showDialogId:'firstLevelDialog',
						searchID:'searchGoods',
						resetSearchID:'resetsearchGoods',
						searchFormID:'searchGoodsForm',
						ctx:'${ctx}'
					});
			//初始化查询
			init(goodsParamObj);
			//setTimeout(function(){
				//$.parser.parse(".optionsAreas");
			//},1000);
		});
		
		function transApproveStatus(value,row,index){
			if(value == 0){
				return "待审核";
			}else if(value == 1){
				return "已通过";
			}
		}
		
		function transIsShelve(value, row, index){
			if(value == 0){
				return "未上架";
			}else if(value == 1){
				return "已上架";
			}else if(value == 2){
				return "已下架";
			}
		}
		
		/**
		 * 返回操作按钮
		 * @param {Object} value
		 * @param {Object} row
		 * @param {Object} index
		 */
		function getOptionsBtn(value, row, index){
			return '<a href="${ctx }/web/goodsInfo?goodsId='+row.id+'&disType=view" target=_blank class="easyui-linkbutton">商品浏览</a>';
		}
		
		/**
		 * 商品上下架
		 */
		function putGoodsOnShelve(type){
			var records = $('#'+goodsParamObj.tableId).datagrid('getSelections');
			if(records.length < 1){
				$.messager.alert('提示',"请选择记录！",'info');
			}else{
				var optionIds = '';
				var ifValid = true;
				var isShel = true;
				var info = '';
				for(var i = 0; i < records.length ; i++){
					if(!records[i].approveStatus || (records[i].approveStatus != 2 && records[i].approveStatus != 4)){// 未审核通过
						ifValid = false;
						break;
					}
					if(records[i].isShelve == 2){
						isShel = false;
						break;
					}
					optionIds += records[i].id+",";
				}
				if(!ifValid){
					$.messager.alert('提示',"未审核通过的商品无法进上下架操作！",'info');
					return;
				}
				if(!isShel){
					$.messager.alert('提示',"您选择的商品不是已上架的，操作失败！",'info');
					return;
				}
				if(type == 1){
					info = '确定商品上架吗？';
				}else{
					info = '确定商品下架吗？';
				}
				optionIds = optionIds.substring(0,optionIds.length - 1);
				
				$.messager.confirm('提示',info,function(result){
				if(result){
					$.ajax({
						type: "post",
						url:  "${ctx}/business/goodsManage/goodsShelveUpOrDown",
						data:'optionIds=' + optionIds + "&type=" + type,
						success: function(data,res){
							var result = $.trim(res.responseText);
							if(result == "unLogin"){
								var msn = ''||i18n[''];
								msn = msn||i18n[result];
								if(msn != undefined){
									$.messager.alert('提示',msn,'info',function(){top.location='${ctx}/manage';});
									return ;
								}
							}
							var msn = msn||i18n[data];	
							if(msn == undefined){
								$.messager.alert('提示',i18n.serverError,'error');
							}else if(msn)	{
								$.messager.alert('提示',msn,'info',function(){$('#'+goodsParamObj.tableId).datagrid('reload');});
							}
						},
						error: function(res){
							$.messager.alert('提示',i18n.requestError,'error');
						}
					});
				}});
			}
		}
		
		function bindPictures(id){
			$("#bindPicDialog").dialog({
				title:"商品绑定图片资源",  
			    iconCls:'icon-view',
			    width: 1000,    
			    height: 440,    
			    closed: false,    
			    inline : true,
			    cache: false,    
			    href: "${ctx}/business/goodsManage/viewPicOfGoods?bindPicGoodsId=" + id,    
			    modal: true,
				extractor : function(data){
					if($.trim(data) == "unLogin"){
						var msn = ''||i18n[''];
						msn = msn||i18n[data];
						if(msn != undefined){
							$.messager.alert('提示',msn,'info',function(){top.location='${ctx}/manage';});
							return '';
						}
					}
					return data;
				},
			    buttons : []
			})
		}
		
		function selectParentgoodsType(){
			selgoodsTypePidGoodsTypeTree($('#goodsTypePid').val());
			$('#goodsTypePidGoodsTypeTreeDialog').dialog('open');
		}
	</script>
</html>
<div id='goodsTypePidTool' style='padding:5px;display: none;'>
	<div style='margin-bottom:5px'>
		节点名称
		<input type='text' id='selgoodsTypePidGoodsTypeTreeName' name='treeName'/>&nbsp;&nbsp;
		<a href='javascript:void(0);' onclick='selgoodsTypePidGoodsTypeTree();' class='easyui-linkbutton' >查询</a>
	</div>
</div>
<div id="goodsTypePidGoodsTypeTreeDialog" class="easyui-dialog" title="选择树" style="width:320px;height:442px;padding:8px;padding-top: 0px;"
		data-options="iconCls: 'icon-menu-icon',method:'post',closed:true,modal: true,toolbar:'#goodsTypePidTool',
		buttons: [{	
			text:'选择',
			iconCls:'icon-ok',	
			handler:function(){
				getSelectgoodsTypePidGoodsTypeNode();	
				}
			},{	
			text:'取消',	
			iconCls:'icon-cancel',
			handler:function(){		
				$('#goodsTypePidGoodsTypeTreeDialog').dialog('close');	
				}
			}]">
		<ul id="goodsTypePidGoodsTypeTree" class="easyui-tree" data-options="url:'',method:'get',animate:true,loadFilter:function(data){
				ids=data.selIds;return data.tree;
			},formatter:function(node){	
				if(ids != undefined){	
					for(var i=0;i<ids.length;i++){		
						if(node.id == ids[i]){		
								return '<span style=\'color:red;font-weight: bold;\'>'+node.text+'</span>';	
						}		
					}	
				}	return node.text;}">
		</ul>
	</div>
<script type="text/javascript">
	var ids;
	$(function() {
		<c:if test="${not empty goodsType.pid && goodsType.pid=='1'}">
		var showDoc;
		showDoc = '<th align="right" width="10%" id="typeNumTh">商品类型编号</th>';
		showDoc += ' <td id="typeNumTd"><input name="typeCode" class="easyui-validatebox" size="30" value="${goodsType.typeCode}" data-options="required:true,validType:[\'unsafe\']" maxlength="11"/></td>';
		$("#goodsTypeNum").append(showDoc);
		</c:if>		
	});
	function getSelectgoodsTypePidGoodsTypeNode(){	
		var node = $('#goodsTypePidGoodsTypeTree').tree('getSelected');
			if(node == null){		
				$.messager.alert('提示','请选择节点！','info');	
			}else{		
				$('#pName').val(node.text);
				$('#pName').focus();		
				$('#goodsTypePid').val(node.id);
				$('#goodsTypePidGoodsTypeTreeDialog').dialog('close');
			}
		}
	function getSelectsgoodsTypePidGoodsTypeNode(){
		var nodes = $('#goodsTypePidGoodsTypeTree').tree('getChecked');
		var nodeNames = '';
		var nodeIds = '';	
		for(var i=0; i<nodes.length; i++){		
			if (nodeNames != '') 
				nodeNames += ',';	
				nodeNames += nodes[i].text;		
			if (nodeIds != '') 
				nodeIds += ',';		
				nodeIds += nodes[i].id;	
		}	
		$('#pName').val(nodeNames);	
		$('#goodsTypePid').val(nodeIds);	
		$('#goodsTypePidGoodsTypeTreeDialog').dialog('close');
	}
	function selgoodsTypePidGoodsTypeTree(selectIds){
		$('#goodsTypePidGoodsTypeTree').tree('options').url='${ctx}/business/goodstype/getGoodsTypeTreeDialog?r='+new Date().getTime()+'&nodeName='+encodeURI(encodeURI($('#selgoodsTypePidGoodsTypeTreeName').val()))+'& deleteIds ='+selectIds;
		$('#goodsTypePidGoodsTypeTree').tree({
			onLoadSuccess:function(node, data){
				var node = $('#goodsTypePidGoodsTypeTree').tree('find', selectIds);
				if(node){
					$('#goodsTypePidGoodsTypeTree').tree('expand', node.target).tree('select', node.target);
				}
			}
		}
	)}
</script>